package service.Impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.User;
import service.UserService;
import utils.Md5Util;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao ud=new UserDaoImpl();
    @Override
    public int init(User user) {
        if(ud.selectName(user.getUsername())==null){
            if(ud.selectEmail(user.getEmail())==null){
                user.setPassword(Md5Util.stringMd5(user.getPassword()));
                int i=ud.init(user);
                return i;
            }else{
                return 3;//邮箱已被注册
            }
        }else{
            return 2;//用户名已注册
        }
    }

    @Override
    public User selectEmail(String email) {
        return ud.selectEmail(email);
    }

    @Override
    public User selectName(String username) {
        return ud.selectName(username);
    }

    @Override
    public User selectID(int id) {
        return ud.selectID(id);
    }

    @Override
    public int update(User user) {
        if(ud.selectName2(user.getUsername(),user.getId())==null){
            if(ud.selectEmail2(user.getEmail(),user.getId())==null){
                int i=ud.update(user);
                System.out.println(i);
                return i;
            }else{
                System.out.println("3");
                return 3;//邮箱已被注册
            }
        }else {
            System.out.println("2");
            return 2;//用户名已注册
        }
    }

    @Override
    public List<User> selectAll() {
        return ud.selectAll();
    }

    @Override
    public User denglu(String email, String password) {
        User user=ud.selectEmail(email);
        if(user!=null){
            password=Md5Util.stringMd5(password);
            if(user.getPassword().equals(password)){
                return user;
            }
        }
        return null;
    }

    @Override
    public int update1(String p, int id) {
        return ud.update1(p,id);
    }
}
